package com.andy.utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.andy.vo.ExamTestVO;

public class TestUtils {

	public static final int MULTI_TEST_COUNT = 10;	// 多选题固定数量为１０
	
	public static final int MULTI_TEST_TYPE = 2;	// 多选题
	
	public static final int SINGLE_TEST_TYPE = 1;	// 单选题
	
	/**
	 * 校验科目分数和数量
	 * @return
	 */
	public static boolean checkSubjectScoreAndCount(int totalTestCount, int totalTestScore, 
			Map<Integer, Integer> testTypeScoreMap) {
		// 总题数小于多选题固定数
		if (totalTestCount < MULTI_TEST_COUNT) {
			return false;
		}
		
		if (testTypeScoreMap == null) {
			return false;
		}
		
		// 校验最小总分
		Integer multiTestScore = testTypeScoreMap.get(MULTI_TEST_TYPE);
		if (multiTestScore == null) {
			return false;
		}
		if (totalTestScore < MULTI_TEST_COUNT * multiTestScore) {
			return false;
		}
		
		Integer singleTestScore = testTypeScoreMap.get(SINGLE_TEST_TYPE);	//　获取单选题分数
		if (singleTestScore == null) {
			return false;
		}
		
		// 计算单选题数量
		Integer singleTestCount = totalTestCount - MULTI_TEST_COUNT;
		Integer singleTestSubjectTotalScore = totalTestScore - (MULTI_TEST_COUNT * multiTestScore);		// 科目总分　－　多选题总分
		Integer singleTestOwnTotalScore = singleTestCount * singleTestScore;		// 科目总分　－　多选题总分
		
		if (singleTestSubjectTotalScore.compareTo(singleTestOwnTotalScore) != 0) {
			return false;
		}
		
		return true;
	}
	
	/**
	 * 计算得到各个题型的数量
	 * @param totalTestCount
	 * @param testTypeScoreMap
	 * @return
	 */
	public static Map<Integer, Integer> getTestCountByScore(int totalTestCount,
			Map<Integer, Integer> testTypeScoreMap) {
		Map<Integer, Integer> resultMap = new HashMap<Integer, Integer>();
		for (Integer key : testTypeScoreMap.keySet()) {
			if (key.compareTo(MULTI_TEST_TYPE) == 0) {
				resultMap.put(MULTI_TEST_TYPE, MULTI_TEST_COUNT);
			} else if (key.compareTo(SINGLE_TEST_TYPE) == 0) {
				resultMap.put(SINGLE_TEST_TYPE, totalTestCount - MULTI_TEST_COUNT);
			}
		}
		return resultMap;
	}
	
	public static List<Integer> getRandomTestIdList(List<ExamTestVO> testList, Integer randomCount) {
		List<Integer> resultList = new ArrayList<Integer>();
		int testListTotalCount = testList.size();
		// 3.1. 判断数据库里的题目数量是否足够
		if (testList != null) {
			if (randomCount.compareTo(testList.size()) > 0) {
				return null;
			}
		}
		for (int i = 0; i < randomCount; i ++) {	// 取randomCount数量的题目号
			// 1. 生成随机下标
			int randomIndex = (int) (testListTotalCount * Math.random());
			
			// 2. 取得题目，把题目id加入resultList
			ExamTestVO testVO = testList.get(randomIndex);
			if (testVO != null) {
				resultList.add(testVO.getId());
			}
			
			// 3. 移除已经加入的题目
			testList.remove(randomIndex);
			testListTotalCount = testList.size();	//	更新总数
		}
		return resultList;
	}

}
